package vashion.azeroth.core.manager.item;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.ItemYvogueForestDao;
import vashion.azeroth.core.item.pojo.ItemYvogueForestDO;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.result.ApiResult;

/**
 * Created by Loki on 17/4/26. yvogue类目管理
 */
@Service
public class ItemYvogueForestManager extends BaseManagerImpl<ItemYvogueForestDO> {

	private final static Logger log = LoggerFactory.getLogger(ItemYvogueForestManager.class);

	@Autowired
	private ItemYvogueForestDao itemYvogueForestDao;

	@Override
	public BaseDao<ItemYvogueForestDO> getDao() {
		return itemYvogueForestDao;
	}

	/**
	 * 按照yvogue类目名精确查找类目类
	 * 
	 * @param yvogueForestTitle
	 * @return yvogue类目名为yvogueForestTitle的类目类
	 */
	public ApiResult<ItemYvogueForestDO> findByYvogueCataTitle(String yvogueForestTitle) {
		try {
			ItemYvogueForestDO itemYvogueForestDO = itemYvogueForestDao.findByYvogueCataTitle(yvogueForestTitle);
			return ApiResult.newSuccessResult(itemYvogueForestDO);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 保存yvogue类目和本系统的类目的关联
	 * 
	 * @param itemYvogueForestDO
	 * @return yvogue类目和系统类目关联保存结果
	 */
	public ApiResult<Boolean> publishYvogueForest(ItemYvogueForestDO itemYvogueForestDO) {
		ApiResult<ItemYvogueForestDO> yvoguoForestResult = this
				.findByYvogueCataTitle(itemYvogueForestDO.getYvogueCataTitle());
		try {
			if (yvoguoForestResult.isSuccess()) {
				if (null == yvoguoForestResult.getData()) {
					itemYvogueForestDao.save(itemYvogueForestDO);
					return ApiResult.newSuccessResult(true);
				} else {
					return ApiResult.newSuccessResult(false);
				}
			} else {
				return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
						GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

}
